package com.syqc.monitor.ui.tyre

import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.PopupWindow
import com.syqc.monitor.R
import com.syqc.monitor.databinding.IncludeNegativePositiveBinding
import com.syqc.monitor.databinding.PopupTyreBinding

/**
 *@author joker
 *@time   2024/3/4 9:33
 *@desc
 */
class TyrePopup(context: Context, callback: (type: Int, isTempC: Boolean) -> Unit) : PopupWindow() {
    private val binding = PopupTyreBinding.inflate(LayoutInflater.from(context))
    private val npBinding = IncludeNegativePositiveBinding.bind(binding.root)
    private var type: Int = 0
    private var isTempC: Boolean = true

    init {
        contentView = binding.root
        isClippingEnabled = false
        isFocusable = true //popupwindow设置焦点
        setBackgroundDrawable(ColorDrawable(-0x56000000)) //设置背景
        isOutsideTouchable = true //点击外面窗口消失
        width = LinearLayout.LayoutParams.MATCH_PARENT //设置SelectPicPopupWindow弹出窗体的宽
        height = LinearLayout.LayoutParams.MATCH_PARENT //设置SelectPicPopupWindow弹出窗体的高

        binding.rgTemp.setOnCheckedChangeListener { _, checkedId ->
            isTempC = checkedId == R.id.rdbTempC

        }

        binding.rgPressure.setOnCheckedChangeListener { _, checkedId ->
            type = when (checkedId) {
                R.id.rdbPKpa -> 0
                R.id.rdbPPsi -> 1
                R.id.rdbPKgcm -> 2
                else -> 3
            }
        }

        npBinding.tvNegative.setOnClickListener {
            callback(type, isTempC)
            dismiss()
        }
        npBinding.tvPositive.setOnClickListener {
            dismiss()
        }

    }

    fun showLocation(view: View, isTempC: Boolean = true, type: Int = 0) {
        this.type = type
        this.isTempC = isTempC
        binding.rgTemp.check(if (isTempC) R.id.rdbTempC else R.id.rdbTempF)
        binding.rgPressure.check(
            when (type) {
                0 -> R.id.rdbPKpa
                1 -> R.id.rdbPPsi
                2 -> R.id.rdbPKgcm
                else -> R.id.rdbPBar
            }
        )
        showAtLocation(view, Gravity.NO_GRAVITY, 0, 0)
    }


}